package com.ym.soso.enums;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.util.StdConverter;
import com.ym.common.annotations.EnumDescription;
import com.ym.common.constant.DictEnum;

import java.util.ArrayList;
import java.util.List;


@EnumDescription("特色服务")
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum SpecServiceEnum implements DictEnum {

    //--------------------start-----------------

    MEDICARE_FIXED_POINT("000001", "医保定点"),

    MIGRATORY_BIRD_HOUSE("000010", "候鸟旅居"),

    OFFSITE("000100", "异地收住"),

    CERTIFICATION_FIRST("001000", "认证优先"),

    BRAND_CHAIN("010000", "品牌连锁"),

    LIVE_EXPERIENCE("100000", "试住体验"),
    //--------------------end-------------------
    ;

    private String key;

    private String value;

    SpecServiceEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String getKey() {
        String key = this.key;
        Integer keys = Integer.parseInt(key, 2);
        return String.format("%02d", keys);
    }

    @Override
    public String getValue() {
        return this.value;
    }

    /**
     * 根据key获取服务类型
     *
     * @param binaryStr 二进制，转换为key  "000001" -> "01"
     * @return
     */
    public static String getKeys(String binaryStr) {
        Integer keys = Integer.parseInt(binaryStr, 2);
        return String.format("%02d", keys);
    }


    /**
     * 根据key获取服务类型
     *
     * @param key 十进制，翻译使用 "01" -> 医保定点
     * @return
     */
    public static List<DictEnum> getValues(String key) {
        String keys = String.format("%06d", Integer.parseInt(Integer.toBinaryString(Integer.parseInt(key))));
        return getValuesByBinary(keys);
    }

    /**
     * 根据key获取服务类型
     *
     * @param binaryStr 二进制字符串，翻译使用  "000001" -> "医保定点"
     * @return
     */
    public static List<DictEnum> getValuesByBinary(String binaryStr) {
        char[] chars = binaryStr.toCharArray();
        List<DictEnum> specServiceEnums = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]=='0'){
                continue;
            }
            switch (String.valueOf(i)) {
                case "0":
                    specServiceEnums.add(SpecServiceEnum.LIVE_EXPERIENCE);
                    break;
                case "1":
                    specServiceEnums.add(SpecServiceEnum.BRAND_CHAIN);
                    break;
                case "2":
                    specServiceEnums.add(SpecServiceEnum.CERTIFICATION_FIRST);
                    break;
                case "3":
                    specServiceEnums.add(SpecServiceEnum.OFFSITE);
                    break;
                case "4":
                    specServiceEnums.add(SpecServiceEnum.MIGRATORY_BIRD_HOUSE);
                    break;
                case "5":
                    specServiceEnums.add(SpecServiceEnum.MEDICARE_FIXED_POINT);
                    break;
            }
        }
        return specServiceEnums;
    }

    /**
     * 序列化使用
     */
    public static class Covert extends StdConverter<String, List<DictEnum>> {
        @Override
        public List<DictEnum> convert(String value) {
            return getValues(value);
        }
    }
}
